package com.example.zheng.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zheng.entity.Repairman;
import com.example.zheng.service.IRepairmanService;
import com.example.zheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2022/1/19 - 22:35
 */
@RestController
public class RepairmanController {

    @Autowired
    private IRepairmanService iRepairmanService;
    @GetMapping("/getAllRepairmanType")
    public Result getAllRepairmanType(){
        QueryWrapper wrapper = new QueryWrapper();
        //wrapper.orderBy(true,true,"repairman_repairtype");
        wrapper.select("DISTINCT repairman_repairtype");
        List<Repairman> list = iRepairmanService.list(wrapper);
        List<String> collect = list.stream().map(repairman -> repairman.getRepairmanRepairtype()).collect(Collectors.toList());
        //System.out.println(collect);
        return Result.success("获取维修员类型成功",collect);
    }

    @GetMapping("/getRepairmanByType")
    public Result getRepairmanByType(String repairType){
        System.out.println("repairType==="+repairType);
        QueryWrapper wrapper = new QueryWrapper();
        //wrapper.orderBy(true,true,"repairman_repairtype");
        wrapper.select("repairman_id","repairman_name");
        wrapper.eq("repairman_repairtype",repairType);
        List<Repairman> list = iRepairmanService.list(wrapper);
        System.out.println(list);
        return Result.success("获取维修员成功",list);
    }

    @GetMapping("/getRepairmanById/{id}")
    public Result getRepairmanById(@PathVariable(name = "id") Integer id){
        Repairman repairman = iRepairmanService.getById(id);
        repairman.setRepairmanPassword(null);
        return Result.success(repairman);
    }

    @GetMapping("/getAllRepairman")
    public Result getAllRepairman(@RequestParam(name = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                  @RequestParam(name = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                                  String repairmanName){
        QueryWrapper<Repairman> wrapper = new QueryWrapper<>();
        wrapper.like("repairman_name",repairmanName);

        Page<Repairman> page = new Page<>(pageNum,pageSize);
        Page<Repairman> page1 = iRepairmanService.page(page, wrapper);

        return Result.success("获取成功",page1);
    }

    @PostMapping("/addRepairman")
    public Result addRepairman(@RequestBody Repairman repairman){
        System.out.println("repairman==="+repairman);
        boolean save = iRepairmanService.save(repairman);
        if (!save){
            return Result.failure("添加失败");
        }
        return Result.success("添加成功");
    }

    @DeleteMapping("/deleteRepairmanById/{id}")
    public Result deleteRepairmanById(@PathVariable(name = "id") Integer id){
        boolean remove = iRepairmanService.removeById(id);
        if (!remove){
            return Result.failure("删除失败");
        }
        return Result.success("删除成功");
    }

    @PutMapping("/editRepairmanById/{id}")
    public Result editRepairmanById(@RequestBody Repairman repairman,
                                    @PathVariable("id") Integer id){
        System.out.println("Repairman--editRepairmanById=="+repairman);
        repairman.setRepairmanId(id);
        iRepairmanService.updateById(repairman);
        return Result.success("信息编辑成功");
    }

    @GetMapping("/getRepairmanCount")
    public Result getRepairmanCount(){
        int count = iRepairmanService.count();
        return Result.success("获取成功",count);
    }

}
